<?php
/**
 * 登录、注册、退出控制器
 * User: pengyixing
 * Date: 18/8/24
 * Time: 23:59
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\Request;

class Login extends Controller
{

    public function _initialize()
    {
        $this->view->engine->layout('layout/login');
    }

    /**
     * 登录
     * @return \think\response\View
     */
    public function signIn(Request $request)
    {
        if (!$request->isAjax()) {
            $this->assign('title', '登录');
            return view();
        } else {
            $this->doSignIn($request->post());
        }


    }

    private function doSignIn($args)
    {
//        try {
//            $account = safeString(trim($args['account']));
//            $passwd = safeString(trim($args['password']));
//            $isSignIn = (new \app\Lib\Model\User\SignIn())
//                ->setAccount($account)
//                ->setPassword($passwd)
//                ->signIn();
//            if (!$isSignIn) {
//                throw new Exception('登录失败');
//            }
//            UserCommon::regLoginSession($isSignIn);
//            $url = url('admin/Index/index');
//            $this->diesuccess('登录成功', 0, $url);
//
//        } catch (Exception $e) {
//            $this->dieerror($e->getMessage());
//        }
    }

    /**
     * 注册
     * @param Request $request
     * @return \think\response\View
     */
    public function signUp(Request $request)
    {
        if (!$request->isAjax()) {
            $this->assign('title', '注册');
            return view();
        }
        $this->doSignUp($request->post());

    }

    /**
     * 执行注册
     */
    private function doSignUp($args)
    {
//        try {
//            Db::transaction(function () use ($args) {
//                $account = safeString(trim($args['account']));
//                $password = safeString(trim($args['password']));
//                $againPassword = safeString(trim($args['againPassword']));
//                $uid = (new \app\Lib\Model\User\SignUp())
//                    ->setAccount($account)
//                    ->setPassword($password)
//                    ->setConfirmPassword($againPassword)
//                    ->exec();
//
//                // 注册成功执行登录
//                if ($uid) {
//                    $isLogin = (new SignIn())->setUid($uid)->autoSignIn();
//                    UserCommon::regLoginSession($isLogin);
//                }
//                return true;
//            });
//
//            $url = url('admin/Index/index');
//            $this->diesuccess('注册成功', 0, $url);
//        } catch (Exception $e) {
//            $this->dieerror($e->getMessage());
//        }

    }

    /**
     * 退出
     * @throws Exception
     */
    public function signOut()
    {
//        $uid = UserCommon::getUid();
//        $User = new User($uid);
//        $isOut = (new SignOut($User))->exec();
//        $this->success('退出成功', 'Login/SignIn');
    }

    private function diesuccess($des, $code = 0, $data = [], $count = 0)
    {
        header("Content-type: application/json");
        $data = [
            'code' => $code,
            'msg' => $des,
            'count' => $count,
            'data' => $data,
        ];
        exit(json_encode($data));
    }

    private function dieerror($des, $code = 1, $data = [], $count = 0)
    {
        header("Content-type: application/json");
        $data = [
            'code' => $code,
            'msg' => $des,
            'count' => $count,
            'data' => $data,
        ];
        exit(json_encode($data));

    }

}